package com.yzz.composeweatherproject.activity

import android.util.Log
import androidx.compose.foundation.interaction.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import com.google.accompanist.pager.*
import com.yzz.composeweatherproject.IDataSuccessListener
import com.yzz.composeweatherproject.activity.side_effects.MoviesScreen
import com.yzz.composeweatherproject.activity.side_effects.MyScreen
import com.yzz.composeweatherproject.activity.side_effects.UiBean
import com.yzz.composeweatherproject.activity.side_effects.UiState
import com.yzz.composeweatherproject.activity.ui.theme.ComposeWeatherProjectTheme
import com.yzz.composeweatherproject.entity.LocationBean
import com.yzz.composeweatherproject.http.okhttp.OkHttpUtils
import kotlinx.coroutines.delay
import java.lang.Exception

class PageActivity : BaseActivity() {

    @Composable
    override fun initView() {
//        setPage()
        NavPageView()
    }

}


@OptIn(ExperimentalPagerApi::class)
@Composable
private fun setPage() {
    val pageList = arrayListOf("1111", "2222", "3333", "4444", "55555")
    //viewpager 的 remember
    val pagerState = rememberPagerState()
    val currentRememberList = ArrayList<MutableState<Boolean>>()
    //这就相当于viewpager 的 registerPageChangeListener 接口，页面更改会触发的回调
    LaunchedEffect(pagerState) {
        // 从寻呼机状态中收集读取当前页面的快照流
        snapshotFlow { pagerState.currentPage }.collect { page ->
            currentRememberList.forEachIndexed { index, mutableState ->
                mutableState.value = index == page
            }
        }
    }
    Column {
        //tab 顶部标签控件
        TabRow(
            // 我们选择的标签是我们当前的页面
            selectedTabIndex = pagerState.currentPage,
            // 使用提供的 pagerTabIndicatorOffset 修饰符覆盖指标
            indicator = { tabPositions ->
                TabRowDefaults.Indicator(
                    Modifier.pagerTabIndicatorOffset(pagerState, tabPositions)
                )
            },
            contentColor = Color.Transparent
        ) {
            // 为我们所有的页面添加标签
            pageList.forEachIndexed { index, title ->
                //用于初始化，选项器是否被选择
                val currentRemember = remember {
                    mutableStateOf(pagerState.currentPage == index)
                }
                currentRememberList.add(currentRemember)
                Column {
                    Tab(
                        text = { Text(title) },
                        selected = currentRemember.value,
                        onClick = {
                            currentRememberList.forEach {
                                it.value = false
                            }
                            currentRemember.value = true
                        },
                        selectedContentColor = Color.Red,
                        unselectedContentColor = Color.White,
                    )
                    if (currentRemember.value) {
                        Divider(color = Color.Red, thickness = 1.dp)
                    }
                }

            }
        }
        //横向 viewpager 控件
        HorizontalPager(count = pageList.size, state = pagerState) { page ->
//            Column {

//                Log.d("yyzzpage", page.toString())
//            }
            LazyColumn(modifier = Modifier.fillMaxSize()) {
                item{
                    // 我们的页面内容
                    Text(
                        text = "Page: ${pageList[page]}",
                        modifier = Modifier.fillMaxSize()
                    )
                }

                items(50) {
                    Text(text = it.toString())
                }
            }

        }
    }


}

private fun getSelectPosition(list: ArrayList<MutableState<Boolean>>): Int {
    list.forEachIndexed { index, mutableState ->
        if (mutableState.value) {
            return index
        }
    }
    return 0
}

fun isTabSelect(b: MutableState<Boolean>, page: Int, index: Int): Boolean {
    b.value = page == index
    return b.value
}

@Composable
fun Greeting3(name: String) {
    Text(text = "Hello $name!")
}

private fun requestData() {
    OkHttpUtils.instance.getLocationUrl(object : IDataSuccessListener<LocationBean> {
        override fun onSuccess(data: LocationBean?) {
            Log.d("yyzzhttp", "content")

            if (data != null) {
            } else {

            }
        }

        override fun onFail(msg: String?) {
        }

    })

}

@Preview(showBackground = true)
@Composable
fun DefaultPreview3() {
    ComposeWeatherProjectTheme {
        setPage()
    }
}